package com.vodafone.nocs.inops.tools.test.ldap;

import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPException;

public class LdapWrapper {

	String host;
	int port;
	String authid;
	String authpw;
	String base;
	LDAPConnection ld;
	
	public void connectLdap(){
		synchronized (this){
			if ( ld!=null  && ld.isConnected() ){
				return;
			}
			//if (ld == null)
			ld = new LDAPConnection();
			connectAndBind(ld, host, port, authid, authpw);
		}
	}
	
	public static void connectAndBind(LDAPConnection ld, String host, int port, String authid, String authpw) {
		int status = -1;
		try {
			// Connect to the LDAP Server
			ld.connect(host, port, authid, authpw);

//			System.out
//					.println("Connected ---------------------------------------------");

			ld.bind(2, authid, authpw);
//			System.out
//					.println("Binded ---------------------------------------------");

		} catch (LDAPException e) {
			System.out.println(e.toString());
		}

	}


	public LdapWrapper(String host, int port, String authid, String authpw,
			String base) {
		super();
		this.host = host;
		this.port = port;
		this.authid = authid;
		this.authpw = authpw;
		this.base = base;
		this.ld = ld;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public String getAuthid() {
		return authid;
	}

	public String getAuthpw() {
		return authpw;
	}

	public String getBase() {
		return base;
	}

	public LDAPConnection getLd() {
		return ld;
	}

	
}
